package com.xjve.framework.hibernate.jpa;

import org.springframework.data.jpa.repository.JpaRepository;

public class CurdEntity {
    /**
     * 保存实体
     */
    @SuppressWarnings("unchecked")
    public <T extends BaseEntity> T save() {
        JpaRepository<T, Long> repository = RepositoryRegistry.getRepository((Class<T>) this.getClass());
        if (repository != null) {
            return repository.save((T) this);
        }
        throw new RuntimeException("No repository found for entity: " + this.getClass().getSimpleName());
    }


    /**
     * 删除实体
     */
    @SuppressWarnings("unchecked")
    public <T extends BaseEntity> void delete() {
        JpaRepository<T, Long> repository = RepositoryRegistry.getRepository((Class<T>)this.getClass());
        if (repository != null) {
            repository.delete((T) this);
        } else {
            throw new RuntimeException("No repository found for entity: " + this.getClass().getSimpleName());
        }
    }
}
